<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
 <div id="createStrategy" title="添加策略" style="display:none">
	  	    	<div>策略名称：<input name="strategy.fieldName" class="textInfo" maxlength="100" id="createFieldName"/><span id="createFieldNameMsg"></span></div>
	  	    	<div style="margin-top:2px">策 &nbsp;&nbsp;&nbsp;&nbsp;略：
	  	    	<select name="strategy.strategy"  id="createStrategy" onchange="changeStrategy('create')">
	  	    		<option value="">请选择</option>
	  	    		<option value="TAG">标签匹配</option>
	  	    		<option value="REGULAR">正则匹配</option>
	  	    		<option value="XPATH">xpath匹配</option>
	  	    	</select>
	  	    	<span id="createStrategyMsg"></span></div>
	  	    	<div id="createTagDiv" style="display:none">
		  	    	<div style="margin-top:2px">开始标签：<input name="strategyValue.startTag" size="60" class="textInfo" id="createStartTag"/>
		  	    	<span id="createStartTagMsg"></span>
		  	    	</div>
		  	    	<div style="margin-top:2px">结束标签：<input name="strategyValue.endTag" size="60" class="textInfo" id="createEndTag"/>
		  	    	<span id="createEndTagMsg"></span>
		  	    	</div>
		  	    </div>
	  	    	<div id="createRegularDiv" style="display:none">
		  	    	<div style="margin-top:2px">正则表达式：<input name="strategyValue.regular" size="60" class="textInfo" id="createRegular"/>
		  	    	<span id="createRegularMsg"></span>
		  	    	</div>
		  	    </div>
		  	    <div id="createXpathDiv" style="display:none">
		  	    	<div style="margin-top:2px">Xpath表达式：<input name="strategyValue.xpath" size="60" class="textInfo" id="createXpath"/>
		  	    	<span id="createXpathMsg"></span></div>
		  	    	<div style="margin-top:2px">Xpath模式：<input name="strategyValue.xpathMode" size="10" class="textInfo" id="createXpathMode"/>
		  	    	<span id="createXpathModeMsg" style="color:red">*若为默认模式不需填写，填写格式为(attr:属性名 或 tag:标签名)</span>
		  	    	</div>
		  	    </div>
		  	
	  	    	<div style="margin-top:2px">分页开始标签：<input name="strategy.pageStartTag" size="40" class="textInfo" id="createPageStartTag"/>
	  	    	<span id="createPageStartTagMsg"></span></div>
	  	    	<div style="margin-top:2px">分页结束标签：<input name="strategy.pageEndTag" size="40" class="textInfo" id="createPageEndTag"/>
	  	    	<span id="createPageEndTagMsg"></span>
	  	    	</div>
		  	    
		  	    <div>采集页数：<input name="strategy.pageNums" class="textInfo" maxlength="100" id="createPageNums"/><span id="createPageNumsMsg" style="color:red">不填或小于等于0时系统自动获取分页数</span></div>
		  	    <div>下载文件：<s:radio name="strategy.createDownload" list="#{'false':'否','true':'是'}" value="'false'" cssClass="createDownload" id="createDownload"/>
		  	    <span id="createDownloadMsg"></span></div>
		  	    <div>文件保存路径：<input name="strategy.downloadDir" class="textInfo" maxlength="100" id="createDownloadDir"/><span id="createDownloadDirMsg"></span></div>
		  	    <div style="margin-top:2px">过滤标签：<input type="checkbox" name="strategy.filterTag" value="a" class="createFilterTag"/>a <input type="checkbox" name="strategy.filterTag" value="img" class="createFilterTag"/>img
		  	    	<input type="checkbox" name="strategy.filterTag" value="div" class="createFilterTag"/>div <input type="checkbox" name="strategy.filterTag" value="script" class="createFilterTag"/>script
		  	    </div>
  	   </div>
  	   <div id="updateStrategy" title="修改策略" style="display:none">
	  	    	<div>策略名称：<input name="strategy.fieldName" class="textInfo" maxlength="100" id="updateFieldName"/><span id="updateFieldNameMsg"></span></div>
	  	    	<div style="margin-top:2px">策 &nbsp;&nbsp;&nbsp;&nbsp;略：
	  	    	<select name="strategy.strategy"  id="updateStrategy" onchange="changeStrategy('update')">
	  	    		<option value="">请选择</option>
	  	    		<option value="TAG">标签匹配</option>
	  	    		<option value="REGULAR">正则匹配</option>
	  	    		<option value="XPATH">xpath匹配</option>
	  	    	</select>
	  	    	<span id="updateStrategyMsg"></span></div>
	  	    	<div id="updateTagDiv" style="display:none">
		  	    	<div style="margin-top:2px">开始标签：<input name="strategyValue.startTag" size="60" class="textInfo" id="updateStartTag"/>
		  	    	<span id="updateStartTagMsg"></span>
		  	    	</div>
		  	    	<div style="margin-top:2px">结束标签：<input name="strategyValue.endTag" size="60" class="textInfo" id="updateEndTag"/>
		  	    	<span id="updateEndTagMsg"></span>
		  	    	</div>
		  	    </div>
	  	    	<div id="updateRegularDiv" style="display:none">
		  	    	<div style="margin-top:2px">正则表达式：<input name="strategyValue.regular" size="60" class="textInfo" id="updateRegular"/>
		  	    	<span id="updateRegularMsg"></span>
		  	    	</div>
		  	    </div>
		  	    <div id="updateXpathDiv" style="display:none">
		  	    	<div style="margin-top:2px">Xpath表达式：<input name="strategyValue.xpath" size="60" class="textInfo" id="updateXpath"/>
		  	    	<span id="updateXpathMsg"></span></div>
		  	    	<div style="margin-top:2px">Xpath模式：<input name="strategyValue.xpathMode" size="10" class="textInfo" id="updateXpathMode"/>
		  	    	<span id="updateXpathModeMsg" style="color:red">*若为默认模式不需填写，填写格式为(attr:属性名 或 tag:标签名)</span>
		  	    	</div>
		  	    </div>
		  	    
	  	    	<div style="margin-top:2px">分页开始标签：<input name="strategy.pageStartTag" size="40" class="textInfo" id="updatePageStartTag"/>
	  	    	<span id="updatePageStartTagMsg"></span></div>
	  	    	<div style="margin-top:2px">分页结束标签：<input name="strategy.pageEndTag" size="40" class="textInfo" id="updatePageEndTag"/>
	  	    	<span id="updatePageEndTagMsg"></span>
	  	    	</div>
		  	    
		  	    <div>采集页数：<input name="strategy.pageNums" class="textInfo" maxlength="100" id="updatePageNums"/><span id="updatePageNumsMsg" style="color:red">不填或小于等于0时系统自动获取分页数</span></div>
		  	    <div>下载文件：<s:radio name="strategy.updateDownload" list="#{'false':'否','true':'是'}" value="'false'" cssClass="updateDownload" id="updateDownload"/>
		  	    <span id="createDownloadMsg"></span></div>
		  	    <div>文件保存路径：<input name="strategy.downloadDir" class="textInfo" maxlength="100" id="updateDownloadDir"/><span id="updateDownloadDirMsg"></span></div>
		  	    <div style="margin-top:2px">过滤标签：<input type="checkbox" name="strategy.filterTag" value="a" class="updateFilterTag"/>a <input type="checkbox" name="strategy.filterTag" value="img" class="updateFilterTag"/>img
		  	    	<input type="checkbox" name="strategy.filterTag" value="div" class="updateFilterTag"/>div <input type="checkbox" name="strategy.filterTag" value="script" class="updateFilterTag"/>script
		  	    </div>
  	   </div>
  	   
  	   <input type="hidden" name="currentTrId" id="currentTrId"/>
  	   
  	<link rel="stylesheet" type="text/css" href="${ctx}/js/jquery-ui/jquery-ui-1.8.11.custom.css"/>
    <script src="${ctx}/js/jquery-ui/jquery-ui-1.8.11.custom.min.js"></script>  
	<script type="text/javascript">
	
		$(function() {
		$("#createStrategy").dialog({
			autoOpen: false,
			width: 600,
			modal: true,
			resizable: false,
			buttons: {
				"添加策略": function() { 
				   if(checkFieldName("create") & checkStrategy("create")) {
				   		createStragegy('create');
				   		clearStragegy("create");
				   		$(this).dialog("close"); 
				   }
				   
				}, 
				" 取消 ": function() { 
				    clearStragegy("create");
					$(this).dialog("close"); 
				}
			}
		});
		
		$("#updateStrategy").dialog({
			autoOpen: false,
			width: 600,
			modal: true,
			resizable: false,
			buttons: {
				"修改策略": function() { 
				   if(checkFieldName("update") & checkStrategy("update")) {
				   		createStragegy('update');
				   		clearStragegy("update");
				   		$(this).dialog("close"); 
				   }
				}, 
				" 取消 ": function() { 
				    clearStragegy("update");
					$(this).dialog("close"); 
				}
			}
		});
	});
	
		function changeURLModel() {
			var autoPage = $(".autoPage:checked").val();
			var htmlContent = "";
			if(autoPage == "true") {
				htmlContent = "<input type='text' name='websiteStrategy.url' class='textInfo' size='80' maxlength='100' id='url' onblur='createUrlParam()'/>"
	 	 					+"<span id='urlMsg' style='color:red'>(使用'{*}'匹配多个URL)</span>";
			} else {
				htmlContent = "<textarea name='websiteStrategy.url' class='textInfo'  id='url' rows='10' cols='100'/></textarea>"
	 	 					+"<span id='urlMsg' style='color:red'>(每行一个网址)</span>";
			}
			$("#urlModel").html(htmlContent);
		}
	
		function createUrlParam() {
			var url = $("#url").val();
			var urlParam = $("#urlParam");
			var position = -1;
			var htmlContent = ""; 
			var index = 0;
			while((position = url.indexOf("{*}", position+1)) != -1) {
			    htmlContent += "<div class='textRow'><div class='textLabel'>URL参数"+(index+1)+"(<span style='color:red'>*</span>)：<input type='hidden' name='websiteStrategy.urlParams["+index+"].paramIndex' value='"+index+"' class='textInfo step' size='5' maxlength='10'/></div><div class='textContent'>开始：<input type='text' name='websiteStrategy.urlParams["+index+"].start' class='textInfo step' size='5' maxlength='10'/>&nbsp;结束：<input type='text' name='websiteStrategy.urlParams["+index+"].end' class='textInfo step' size='5' maxlength='10'/>&nbsp;增减：<input type='text' name='websiteStrategy.urlParams["+index+"].step' class='textInfo step' size='5' maxlength='10'/><span id='stepMsg'></span></div></div>";
				index++;
			}
			urlParam.html(htmlContent);
		}
		
		function addStrategy() {
			$("#createStrategy").dialog("open");
		}
		
		function changeStrategy(opt) {
			var strategy = $("#"+opt+"Strategy option:selected").val();
			var tagDiv = $("#"+opt+"TagDiv");
			var regularDiv = $("#"+opt+"RegularDiv");
			var xpathDiv = $("#"+opt+"XpathDiv");
			tagDiv.hide();
			regularDiv.hide();
			xpathDiv.hide();
			if(strategy == "TAG"){
				tagDiv.show();
			} else if(strategy == "REGULAR") {
				regularDiv.show();
			} else if(strategy == "XPATH") {
				xpathDiv.show();
			}
		}
		
		function clearStragegy(opt) {
		var name = $("#"+opt+"FieldName").val("");
		var strategy = $("#"+opt+"Strategy option:selected").val();
		
		if(strategy == "TAG"){
			$("#"+opt+"StartTag").val("");
			$("#"+opt+"EndTag").val("");
		} else if(strategy == "REGULAR") {
			$("#"+opt+"Regular").val("");
		} else if(strategy == "XPATH") {
		 	$("#"+opt+"Xpath").val("");
		 	$("#"+opt+"XpathMode").val("");
		}
		$("#"+opt+"Strategy option[value='']").attr("selected",true);
		$("."+opt+"FilterTag").removeAttr("checked");
		$("."+opt+"Download[value='false']").attr("checked","true");
		$("#"+opt+"DownloadDir").val("");
		$("#"+opt+"PageStartTag").val("");
		$("#"+opt+"PageEndTag").val("");
		$("#"+opt+"PageNums").val("");
		var tagDiv = $("#"+opt+"TagDiv");
		var regularDiv = $("#"+opt+"RegularDiv");
		var xpathDiv = $("#"+opt+"XpathDiv");
		tagDiv.hide();
		regularDiv.hide();
		xpathDiv.hide();
	}
	
	function createStragegy(opt) {
		var name = filterChar($("#"+opt+"FieldName").val());
		var strategy = filterChar($("#"+opt+"Strategy option:selected").val());
		var download = filterChar($("."+opt+"Download:checked").val());
		var downloadDir = filterChar($("#"+opt+"DownloadDir").val());
		
		var pageStartTag = filterChar($("#"+opt+"PageStartTag").val());
		var pageEndTag = filterChar($("#"+opt+"PageEndTag").val());
		var pageNums = filterChar($("#"+opt+"PageNums").val());
		
		alert(pageStartTag + "---" + pageEndTag + "--" + pageNums);
		
		var strategyParamValues="";
		var trObj = "";
		var listObj = $("#strategyList");
		var inputName = "";
	
		var lastTr = listObj.find("tr:last");
		var lastTrId = "";
		if(opt == "update") {
			lastTrId = $("#currentTrId").val();
		} else {
			lastTrId = lastTr.attr("id");
		}
		var indexs = lastTrId.split("_");
		if(indexs.length == 2) {
			  var index = indexs[1];
			  if(opt == "update") {
					index = parseInt(index);
				} else {
					index = parseInt(index) + 1;
				}
			  
			  inputName = "baseStrategies["+index+"]";
			  trObj = "<tr id='index_"+index+"'>";
		 }
		
		
		trObj += "<td class='tdFieldName'><input type='hidden' name='"+inputName+".fieldName' value='"+name+"'/><span>"+name+"</span></td><td class='tdStrategy'><input type='hidden' name='"+inputName+".strategy' value='"+strategy+"'/><span>"+strategy+"</span></td>";
		
		if(strategy == "TAG"){
			var startTag = filterChar($("#"+opt+"StartTag").val());
			var endTag = filterChar($("#"+opt+"EndTag").val());
			
			trObj += "<td class='tdStrategyValue'><input type='hidden' name='"+inputName+".strategyValue.startTag' value='"+startTag+"'/><input type='hidden' name='"+inputName+".strategyValue.endTag' value='"+endTag+"'/><span>"+startTag+","+endTag+"</span></td>";
		} else if(strategy == "REGULAR") {
			var regular = filterChar($("#"+opt+"Regular").val());
			trObj += "<td class='tdStrategyValue'><input type='hidden' name='"+inputName+".strategyValue.regular' value='"+regular+"'/><span>"+regular+"</span></td>";
		} else if(strategy == "XPATH") {
		 	var xpath = filterChar($("#"+opt+"Xpath").val());
		 	var xpathMode = filterChar($("#"+opt+"XpathMode").val());
		 	
		 	trObj += "<td class='tdStrategyValue'><input type='hidden' name='"+inputName+".strategyValue.xpath' value='"+xpath+"'/>";
		 	if($.trim(xpathMode) != "") {
		 		trObj += "<input type='hidden' name='"+inputName+".strategyValue.xpathMode' value='"+xpathMode+"'/><span>"+xpath+","+xpathMode+"</span></td>";
		 	} else {
		 		trObj += "<span>"+xpath+"</span></td>";
		 	}
		 	
		}
		
		trObj += "<td class='tdPageStartTag'><input type='hidden' name='"+inputName+".pageStartTag' value='"+pageStartTag+"'/><span>"+pageStartTag+"</span></td>"
			     +"<td class='tdPageEndTag'><input type='hidden' name='"+inputName+".pageEndTag' value='"+pageEndTag+"'/><span>"+pageEndTag+"</span></td>" 
				 +"<td class='tdPageNums'><input type='hidden' name='"+inputName+".pageNums' value='"+pageNums+"'/><span>"+pageNums+"</span></td>"
				 +"<td class='tdDownload'><input type='hidden' name='"+inputName+".download' value='"+download+"'/><span>"+download+"</span></td>"
			     +"<td class='tdDownloadDir'><input type='hidden' name='"+inputName+".downloadDir' value='"+downloadDir+"'/><span>"+downloadDir+"</span></td>"
		
		var filterTags = $("."+opt+"FilterTag");
		trObj += "<td class='tdFilterTag'>"
		var filterTagValues = "";
		var filterTagInput = "";
		var filterTagText = "";
		filterTags.each(function(i) {
			var filterTag = $(this);
			if(filterTag.attr("checked")) {
				filterTagInput += "<input type='hidden' name='"+inputName+".filterTag["+i+"].tagName' value='"+filterTag.val()+"'/>";
				filterTagText += filterTag.val()+",";
			}
		});
		var lastPosition = filterTagText.lastIndexOf(",");
		if(lastPosition != -1) {
			filterTagText = filterTagText.substring(0, lastPosition);
		}
		trObj += filterTagInput+"<span>"+filterTagText+"</span></td>"
			  +"<td><a href='javascript:updateStrategy(\"index_"+index+"\")'>修改</a>|<a href='javascript:deleteStrategy(\"index_"+index+"\")' onclick='return confirm(\"您真的要做删除操作？\")'>删除</a></td></tr>";
		
		if(opt == "update") {
			lastTrId = $("#currentTrId").val();
			$("#"+lastTrId).replaceWith(trObj); 
		} else {
			listObj.append(trObj);
		}
	}
	
	function updateStrategy(trId) {
			$("#currentTrId").val(trId);
			var currentTr = $("#"+trId);
			var fieldName = $.trim(currentTr.find(".tdFieldName").find("span").html());
			var strategy = $.trim(currentTr.find(".tdStrategy").find("span").eq(0).html());
			var strategyValue = $.trim(currentTr.find(".tdStrategyValue").find("span").eq(0).html());
			var filterTag = $.trim(currentTr.find(".tdFilterTag").find("span").eq(0).html());
			
			var pageStartTag = $.trim(currentTr.find(".tdPageStartTag").find("span").eq(0).html());
			var pageEndTag = $.trim(currentTr.find(".tdPageEndTag").find("span").eq(0).html());
			var pageNums = $.trim(currentTr.find(".tdPageNums").find("span").eq(0).html());
			
			var download = $.trim(currentTr.find(".tdDownload").find("span").eq(0).html());
			var downloadDir = $.trim(currentTr.find(".tdDownloadDir").find("span").eq(0).html());
			
			$("#updateFieldName").val(fieldName);
			
			$("#updatePageStartTag").val(pageStartTag);
			$("#updatePageEndTag").val(pageEndTag);
			$("#updatePageNums").val(pageNums);
			
			$(".uploadDownload").each(function(i) {
				var downloadObj = $(this);
				if(downloadObj.val() == download) {
					downloadObj.attr("checked","true");
				}
			});
			
			$("#updateDownloadDir").val(downloadDir);
			$("#updateStrategy option[value="+strategy+"]").attr("selected",true);
			
			if(filterTag && filterTag != "") {
				var filterTags = filterTag.split(",");
				
				for(var i=0; i<filterTags.length; i++) {
					var checkObj = $(".updateFilterTag[value="+filterTags[i]+"]");
					if(checkObj) {
						checkObj.attr("checked",true);
					}
				}
			}
			
			var tagDiv = $("#updateTagDiv");
			var regularDiv = $("#updateRegularDiv");
			var xpathDiv = $("#updateXpathDiv");
			tagDiv.hide();
			regularDiv.hide();
			xpathDiv.hide();
			if(strategyValue && strategyValue != "") {
				var strategyValues = strategyValue.split(",");
				if(strategy == "TAG"){
					tagDiv.show();
					$("#updateStartTag").val(strategyValues[0]);
					$("#updateEndTag").val(strategyValues[1]);
				} else if(strategy == "REGULAR") {
					regularDiv.show();
					$("#updateRegular").val(strategyValues[0]);
				} else if(strategy == "XPATH") {
					xpathDiv.show();
					$("#updateXpath").val(strategyValues[0]);
					if(strategyValues.length==2) {
					  $("#updateXpathMode").val(strategyValues[1]);
					}
				}
			}
			$("#updateStrategy").dialog("open");
		}
	
	function deleteStrategy(trId) {
			$("#"+trId).remove();
		}
	
	function checkFieldName(opt) {
			 var name = $("#"+opt+"FieldName").val();
		     var nameMsgObj = $("#"+opt+"FieldNameMsg");
		     
		     if($.trim(name) == "") {
		     	nameMsgObj.css("color","red").html("策略字段名称必须指定");
		     	return false;
		     } else {
		     	nameMsgObj.html("");
		     	return true;
		     }
		}
		
	function checkStrategy(opt) {
		    var strategy = $("#"+opt+"Strategy option:selected").val();
			if($.trim(strategy) == "") {
				$("#"+opt+"StrategyMsg").css("color","red").html("必须指定采集策略");
				return false;
			} else if(strategy == "TAG"){
				var startTag = $("#"+opt+"StartTag").val();
				var endTag = $("#"+opt+"EndTag").val();
				
				if($.trim(startTag) == "") {
					$("#"+opt+"StartTagMsg").css("color","red").html("开始标签不能为空");
					return false;
				} else {
					$("#"+opt+"StartTagMsg").html("");
				}
				
				if($.trim(endTag) == "") {
					$("#"+opt+"EndTagMsg").css("color","red").html("结束标签不能为空");
					return false;
				} else {
					$("#"+opt+"EndTagMsg").html("");
				}
				
			} else if(strategy == "REGULAR") {
				var regular = $("#"+opt+"Regular").val();
				if($.trim(regular) == "") {
					$("#"+opt+"RegularMsg").css("color","red").html("正则表达式不能为空");
					return false;
				} else {
					$("#"+opt+"RegularMsg").html("");
				}
			} else if(strategy == "XPATH") {
			 	var xpath = $("#"+opt+"Xpath").val();
			 	if($.trim(xpath) == "") {
					$("#"+opt+"XpathMsg").css("color","red").html("Xpath不能为空");
					return false;
				} else {
					$("#"+opt+"XpathMsg").html("");
				}
			}
			$("#"+opt+"StrategyMsg").html("");
			return true;
		}
		
		function checkWebsiteStrategy() {
			var url = $("#url").val();
			var urlMsg = $("#urlMsg");
			
			var urlRs = true;
			var urlParamRs = true;
	
			if($.trim(url) == "") {
				urlMsg.css("color", "red").html("采集URL必须填写");
				urlRs = false;
			} else {
				if(url.indexOf("{*}") != -1) {
				  var reg = new RegExp("^\\d+$");
				  var steps = $(".step");
				  var rs = true;
				  steps.each(function(i) {
				  	var step = $(this).val();
				  	if(!reg.test($.trim(step))) {
				  		rs = false;
				  	}
				  });
				  if(!rs) {
				  	$("#stepMsg").css("color", "red").html("url参数必须填写，且为数字");
				  	urlParamRs = false;
				  } else {
				  	$("#stepMsg").html("");
				  }
				}
			}
			return urlRs && urlParamRs;
		}
		
		function filterChar(str) {
			if(str) {
				return str.replace("'", "\'").replace('"', '\"');
			} else {
				return "";
			}
		}
	</script>